package com.nebula.controller;


import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.nebula.common.Const;
import com.nebula.common.ServerResponse;
import com.nebula.pojo.Files;
import com.nebula.pojo.User;
import com.nebula.service.IFileService;
import com.nebula.service.IFileUploadService;
import com.nebula.service.IFolderService;
import com.nebula.vo.FileListVo;

@Controller
@RequestMapping("/file")
public class FileController extends Thread{
	
	@Autowired
	private IFileUploadService fileUploadService;
	@Autowired
	private IFileService fileService;
	@Autowired
	private IFolderService folderService;

	@RequestMapping(value = "/fileUpload",method = RequestMethod.POST)
	@ResponseBody
	public ServerResponse<String> fileUpload(@RequestParam (value="file",required=false) MultipartFile fileUpload,
			HttpServletRequest request,HttpSession session){
		User user = (User)session.getAttribute(Const.USERINFO);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String date = sdf.format(new Date());
		String path = "E://nebula/" + date;
		String shortUrl = date;
		return fileUploadService.addFiles(fileUpload, request,path,shortUrl,user);
	}
	
	@RequestMapping(value = "/rename",method = RequestMethod.POST)
	@ResponseBody
	public ServerResponse<String> rename(@RequestBody Files files){
		return fileService.rename(files);
	}
	
	@RequestMapping(value = "/download")
	public void download(HttpServletRequest requset,HttpServletResponse response,
			@RequestParam(value = "fileId") long fileId){
		fileService.download(requset, response, fileId);
	}
	
	@RequestMapping(value = "/compress",method = RequestMethod.POST)
	@ResponseBody
	public ServerResponse<String> compress(HttpServletRequest requset,HttpServletResponse response
			,HttpSession session
			,@RequestParam(value = "fileIdGroup") String fileIdGroup) throws IOException{
		User user = (User) session.getAttribute(Const.USERINFO);
		return fileService.compress(requset, response, fileIdGroup,user);
	}
	
	@RequestMapping(value = "/plDownload")
	public void plDownload(HttpServletRequest requset,HttpServletResponse response
			,HttpSession session,@RequestParam("zipname") String zipname
			,@RequestParam("ctm") String ctm) throws IOException{
		User user = (User) session.getAttribute(Const.USERINFO);
		fileService.plDownload(requset,response, zipname,ctm,user);
	}
	
	@RequestMapping("/picture")
	public String pictureList(HttpServletRequest request,
			@RequestParam(value = "pageNum",defaultValue = Const.DefaultPage.pageNum)int pageNum,
			@RequestParam(value = "pageSize",defaultValue = Const.DefaultPage.pageSize)int pageSize,
			HttpSession session){
		User user = (User)session.getAttribute(Const.USERINFO);
		List<FileListVo> documentList = fileService.pictureList(pageNum, pageSize,user.getUsername(),request);
		
		int parentId = folderService.getAllFilesIdByUsername(user.getUsername());
		
		request.setAttribute("list", documentList);
		request.setAttribute("parentId", parentId);
		return "picture";
	}
	
	@RequestMapping("/document")
	public String documentList(HttpServletRequest request,
			@RequestParam(value = "pageNum",defaultValue = Const.DefaultPage.pageNum)int pageNum,
			@RequestParam(value = "pageSize",defaultValue = Const.DefaultPage.pageSize)int pageSize,
			HttpSession session){
		User user = (User)session.getAttribute(Const.USERINFO);
		List<FileListVo> documentList = fileService.documentList(pageNum, pageSize,user.getUsername(),request);
		
		int parentId = folderService.getAllFilesIdByUsername(user.getUsername());
		
		request.setAttribute("list", documentList);
		request.setAttribute("parentId", parentId);
		return "document";
	}
	
	@RequestMapping("/video")
	public String videoList(HttpServletRequest request,
			@RequestParam(value = "pageNum",defaultValue = Const.DefaultPage.pageNum)int pageNum,
			@RequestParam(value = "pageSize",defaultValue = Const.DefaultPage.pageSize)int pageSize
			,HttpSession session){
		User user = (User) session.getAttribute(Const.USERINFO);
		List<FileListVo> videoList = fileService.videoList(pageNum, pageSize, user.getUsername(), request);
		
		int parentId = folderService.getAllFilesIdByUsername(user.getUsername());
		
		request.setAttribute("list", videoList);
		request.setAttribute("parentId", parentId);
		return "video";
	}
	
	@RequestMapping("/seeds")
	public String seedsList(HttpServletRequest request,
			@RequestParam(value = "pageNum",defaultValue = Const.DefaultPage.pageNum)int pageNum,
			@RequestParam(value = "pageSize",defaultValue = Const.DefaultPage.pageSize)int pageSize
			,HttpSession session){
		User user = (User) session.getAttribute(Const.USERINFO);
		List<FileListVo> videoList = fileService.seedsList(pageNum, pageSize, user.getUsername(), request);
		
		int parentId = folderService.getAllFilesIdByUsername(user.getUsername());
		
		request.setAttribute("list", videoList);
		request.setAttribute("parentId", parentId);
		return "seeds";
	}
	
	@RequestMapping("/music")
	public String musicList(HttpServletRequest request,
			@RequestParam(value = "pageNum",defaultValue = Const.DefaultPage.pageNum)int pageNum,
			@RequestParam(value = "pageSize",defaultValue = Const.DefaultPage.pageSize)int pageSize
			,HttpSession session){
		User user = (User) session.getAttribute(Const.USERINFO);
		List<FileListVo> videoList = fileService.musicList(pageNum, pageSize, user.getUsername(), request);
		
		int parentId = folderService.getAllFilesIdByUsername(user.getUsername());
		
		request.setAttribute("list", videoList);
		request.setAttribute("parentId", parentId);
		return "music";
	}
	
	@RequestMapping("/other")
	public String otherList(HttpServletRequest request,
			@RequestParam(value = "pageNum",defaultValue = Const.DefaultPage.pageNum)int pageNum,
			@RequestParam(value = "pageSize",defaultValue = Const.DefaultPage.pageSize)int pageSize
			,HttpSession session){
		User user = (User) session.getAttribute(Const.USERINFO);
		List<FileListVo> videoList = fileService.otherList(pageNum, pageSize, user.getUsername(), request);
		
		int parentId = folderService.getAllFilesIdByUsername(user.getUsername());
		
		request.setAttribute("list", videoList);
		request.setAttribute("parentId", parentId);
		return "other";
	}
	
	@RequestMapping("/play-video")
	@ResponseBody
	public ServerResponse playVideo(@RequestParam("fileid") Long fileid){
		return fileService.getShortUrlByFileId(fileid);
	}
}
